using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

namespace RelationTextBox
{
    [ComplexBindingProperties("DataSource", "DataMember")]
    class DataGridViewRelationTextBoxControl : RelationTextBox, IDataGridViewEditingControl
    {
        private DataGridView dataGridViewControl;
        private bool valueIsChanged = false;
        private int rowIndexNum;
        
        public DataGridViewRelationTextBoxControl()
        {
            txBRelation.TextChanged += new EventHandler(DisplayTextBox_TextChanged);
            // NU TREBUIE SA FIE INTEGRAT IN CICLUL DE TAB
            this.TabStop = false;
            InitializeComponent();
        }
        /// <summary>
        /// Proprietate pentru a accesa gridul care foloseste controlul
        /// </summary>
        public DataGridView EditingControlDataGridView
        {
            get
            { 
                return (DataGridView)dataGridViewControl;
            }
            set
            { 
                dataGridViewControl = (DataGridView)value;
            }
        }
        void DisplayTextBox_TextChanged(object sender, EventArgs e)
        {
            NotifyDataGridViewOfValueChange();
        }
        /// <summary>
        /// Proprietate care reprezinta valoarea curenta de formatare a controlului de editare
        /// </summary>
        public object EditingControlFormattedValue
        {
            get
            { 
                return GetEditingControlFormattedValue(DataGridViewDataErrorContexts.Formatting);
                //MyTace("EditingControlFormattedValue:" + this.Text);
                //return this.txBRelation.Text;
            }
            set
            { 
                this.Text = (string)value;
                //MyTace("Set_EditingControlFormattedValue:" + value.ToString());
            }
        }
        /// <summary>
        /// Proprietate care reprezinta randul in care se afla controlul de editare
        /// </summary>
        public int EditingControlRowIndex
        {
            get
            { return rowIndexNum;}
            set
            { rowIndexNum = value;}
        }
        /// <summary>
        /// Proprietate care indica daca valoarea din controlul de editare s-a modificat sau nu
        /// </summary>
        public bool EditingControlValueChanged
        {
            get
            { return valueIsChanged;}
            set
            { valueIsChanged = value;}
        }
        /// <summary>
        /// Proprietaea care determina ce cursor tebuie folosit pentru panelul de editare,
        /// i.e. parintele controlului de editare.
        /// </summary>
        public Cursor EditingPanelCursor
        {
            get
            { return base.Cursor;}
        }
        /// <summary>
        /// Proprietate care indica daca controlul de editare trebuie repozitionat la schimbarea 
        /// valorii.
        /// </summary>
        public bool RepositionEditingControlOnValueChange
        {
            get
            { return false;}
        }
        /// <summary>
        /// Metoda apelata de grid inainte de a se afisa controlul ca sa poata fi adaptat 
        /// la stilul de celula furnizat
        /// </summary>
        public virtual void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            if (dataGridViewCellStyle.BackColor.A < 255)
            {
                // The NumericUpDown control does not support transparent back colors
                Color opaqueBackColor = Color.FromArgb(255, dataGridViewCellStyle.BackColor);
                this.BackColor = opaqueBackColor;
                this.dataGridViewControl.EditingPanel.BackColor = opaqueBackColor;
            }
            else
            {
                this.BackColor = dataGridViewCellStyle.BackColor;
            }
            this.ForeColor = dataGridViewCellStyle.ForeColor;
            //this.TextAlign = DataGridViewRelationTextBoxCell.TranslateAlignment(dataGridViewCellStyle.Alignment);
            
        }
        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            //MyTace("GetEditingControlFormattedValue:" + this.dataGridViewControl.CurrentCell.Value.ToString());
            //return this.dataGridViewControl.CurrentCell.Value;
            return this.Text;
        }
        public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
        {
            switch (key & Keys.KeyCode)
            {
                case Keys.Left:
                case Keys.Up:
                case Keys.Down:
                case Keys.Right:
                case Keys.Home:
                case Keys.End:
                case Keys.PageDown:
                case Keys.PageUp:
                    return true;
                default:
                    return false;
            }

        }
        public void PrepareEditingControlForEdit(bool selectAll)
        {
        }
        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            // Let the DataGridView know about the value change
            NotifyDataGridViewOfValueChange();
        }
        //  Notify DataGridView that the value has changed.
        protected virtual void NotifyDataGridViewOfValueChange()
        {
            this.valueIsChanged = true;
            if (this.dataGridViewControl != null)
            {
                this.dataGridViewControl.NotifyCurrentCellDirty(true);
                //MyTace("NotifyCurrentCellDirty(true)");
            }
        }
        /*public void OnMyButtonClick(object sender, EventArgs e)
        {
            if (this.dataGridViewControl != null)
            {
                EventHandler temp = MyButtonClick;
                ///trimite ca si argumente randul si coloana
                if (temp != null)
                {
                    temp(this, new DataGridViewCellEventArgs(this.dataGridViewControl.CurrentCell.ColumnIndex, rowIndexNum));
                    if (this.txBRelation.Text !=
                        this.dataGridViewControl.CurrentCell.Value.ToString())
                        this.txBRelation.Text =
                        this.dataGridViewControl.CurrentCell.Value.ToString();
                    //NotifyDataGridViewOfValueChange();
                }

            }
            

        }*/
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // btnRelation
            // 
            this.btnRelation.Size = new System.Drawing.Size(20, 20);
            // 
            // txBRelation
            // 
            this.txBRelation.Size = new System.Drawing.Size(129, 20);
            // 
            // DataGridViewRelationTextBoxControl
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.Name = "DataGridViewRelationTextBoxControl";
            this.ResumeLayout(false);

        }
        [Description("Indica spre sursa de date a controlului.")]
        [RefreshProperties(RefreshProperties.Repaint)]
        new public ControlBindingsCollection DataBindings
        {
            get
            {
                return base.DataBindings;
            }
        }
        
    }
}
